package server;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import database.local.Business;
import database.local.CheckIn;
import database.local.Comment;
import database.local.Deal;
import database.local.Likes;

public class XmlParser {
	
	public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	
	/*******************************
	 * create Objects from xml 
	 */
	
	
	private static CheckIn getCheckIn(Element checkin_element) {
		String user_name = "", email="", pic="", facebook_link="";
		if (checkin_element == null) return null;
		
		NodeList property_lst = checkin_element.getElementsByTagName("property");
		if (property_lst == null) return null;
		
		for (int property_index = 0; property_index < property_lst.getLength(); property_index++) {
			String property_name = property_lst.item(property_index).getAttributes().item(0).getNodeValue();
			if (property_name.matches("user_name")) {
				user_name = property_lst.item(property_index).getTextContent();
			}
			if (property_name.matches("email")) {
				email = property_lst.item(property_index).getTextContent();
			}
			if (property_name.matches("pic")) {
				pic = property_lst.item(property_index).getTextContent();
			}
			if (property_name.matches("facebook_link")) {
				facebook_link = property_lst.item(property_index).getTextContent();
			}
		}
		CheckIn checkin = new CheckIn(user_name, email, pic, facebook_link);
		return checkin;
	
	}
	/**
	 * parse xml comment
	 * return null if fail
	 */
	private static Comment getComment(Element comment_element) {
		
		if (comment_element == null) return null;
		
		Comment comment = new Comment();
		
		/*get key*/
		comment.setKey(comment_element.getAttributes().getNamedItem("key").getNodeValue());
		NodeList key_lst = comment_element.getElementsByTagName("key");
		comment.setFull_key(key_lst.item(0).getTextContent());
		
		
		NodeList property_lst = comment_element.getElementsByTagName("property");
		if (property_lst == null) return null;
		
		for (int property_index = 0; property_index < property_lst.getLength(); property_index++) {
			String property_name = property_lst.item(property_index).getAttributes().item(0).getNodeValue();
			if (property_name.matches("comment_str")) {
				comment.setComment_str(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("deal_item")) {
				comment.setDeal_item(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("comment_date")) {
				comment.setDate_time(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("comment_rate")) {
				try {
					comment.setRate(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					comment.setRate(0);
				}
			}
			if (property_name.matches("comment_cost_level")) {
				try {
					comment.setCost_level(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					comment.setCost_level(0);
				}
			}
			if (property_name.matches("_source")) {
				comment.setSource(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("_source_type")) {
				comment.setSource_type(property_lst.item(property_index).getTextContent());
			}
			
			
			
		}
		
		return comment;
	}
	
	
	/**
	 * parse xml deal
	 * return null if fail
	 */ 
	private static Deal getDeal(Element deal_element) {
		
		if (deal_element == null) return null;
		
		/*get info*/
		NodeList info = deal_element.getElementsByTagName("dealinfo");
		if (info == null) return null;
		Element el_info = (Element) info.item(0);
		
		NodeList entity = el_info.getElementsByTagName("entity");
		if (entity == null) return null;
		Element el_entity = (Element) entity.item(0);
		
		
		Deal deal = new Deal();
		
		/*get key*/
		deal.setKey(el_entity.getAttributes().getNamedItem("key").getNodeValue());
		NodeList key_lst = el_entity.getElementsByTagName("key");
		deal.setFull_key(key_lst.item(0).getTextContent());
		
		
		NodeList property_lst = el_entity.getElementsByTagName("property");
		if (property_lst == null) return null;
		
		for (int property_index = 0; property_index < property_lst.getLength(); property_index++) {
			String property_name = property_lst.item(property_index).getAttributes().item(0).getNodeValue();
			if (property_name.matches("deal_info")) {
				deal.setDealInfo(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("deal_type")) {
				try {
					deal.setDeal_type(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					deal.setDeal_type(12);
				}
			}
			if (property_name.matches("business_item")) {
				deal.setBusiness_item(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("time")) {
				deal.setTime(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("time_delta")) {
				deal.setDelta_time(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("name")) {
				deal.setName(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("days_bitmap")) {
				try {
					deal.setDays_bitmap(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					deal.setDays_bitmap(SaveZoneHttpConnection.ALL_DAYS);
				}
			}
			if (property_name.matches("rate_sum")) {
				try {
					deal.setRate_sum(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					deal.setRate_sum(0);
				}
			}
			if (property_name.matches("rate_num")) {
				try {
					deal.setRate_num(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					deal.setRate_num(0);
				}
			}
			if (property_name.matches("cost_level_sum")) {
				try {
					deal.setCost_level_sum(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					deal.setCost_level_sum(0);
				}
			}
			if (property_name.matches("cost_level_num")) {
				try {
					deal.setCost_level_num(Integer.parseInt(property_lst.item(property_index).getTextContent()));
				} catch (NumberFormatException e) {
					deal.setCost_level_num(0);
				}
			}
			if (property_name.matches("_source")) {
				deal.setSource(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("_source_type")) {
				deal.setSource_type(property_lst.item(property_index).getTextContent());
			}

			
		}
		
		/*get  comments*/
		List<Comment> comment_lst = deal.getComments();
		NodeList comments = deal_element.getElementsByTagName("comments");
		if (comments == null) return null;
		
		Element el_comments = (Element) comments.item(0);
		NodeList entity_lst = el_comments.getElementsByTagName("entity");
		if (entity_lst == null) return null;
		
		for (int entity_index = 0; entity_index < entity_lst.getLength(); entity_index++){
			el_entity = (Element) entity_lst.item(entity_index);
			Comment comment = getComment(el_entity);
			comment_lst.add(comment);
		}
		
		deal.setComments(comment_lst);
		
		return deal;
	}
	
	/**
	 * parse business xml (including deal list)
	 * @param business_element
	 * @return business object (return null if fail)
	 */
	private static Business getBusiness(Element business_element) {
		
		if (business_element == null) return null;
			
		/*get business info*/
		Business business = new Business();
		NodeList info = business_element.getElementsByTagName("info");
		if (info == null) return null;
		
		Element el_info = (Element) info.item(0);
		
		/*get key*/
		NodeList entity_lst = el_info.getElementsByTagName("entity");
		business.setKey(entity_lst.item(0).getAttributes().getNamedItem("key").getNodeValue());
		
		NodeList key_lst = el_info.getElementsByTagName("key");
		business.setFull_key(key_lst.item(0).getTextContent());
		
		/*get properties*/
		NodeList property_lst = el_info.getElementsByTagName("property");
		
		if (property_lst == null) return null;
		
		for (int property_index = 0; property_index < property_lst.getLength(); property_index++) {
			String property_name = property_lst.item(property_index).getAttributes().item(0).getNodeValue();
			if (property_name.matches("country")) {
				business.setCountry(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("city")) {
				business.setCity(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("street")) {
				business.setStreet(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("street_num")) {
				String street_num = property_lst.item(property_index).getTextContent();
				if (!street_num.matches(""))
					try {
						business.setStreetNum(Integer.parseInt(street_num));
					} catch (NumberFormatException e) {
						business.setStreetNum(0);
					}
			}
			if (property_name.matches("name")) {
				business.setName(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("full_address")) {
				business.setFullAddress(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("latitude")) {
				String latitude = property_lst.item(property_index).getTextContent();
				if (!latitude.matches(""))
					try {
						business.setLatitude(new Integer(Integer.parseInt(latitude)));
					} catch (NumberFormatException e) {
						business.setLatitude(null);
					}
			}
			if (property_name.matches("longitude")) {
				String longitude = property_lst.item(property_index).getTextContent();
				if (!longitude.matches(""))
					try {
						business.setLongitude(new Integer(Integer.parseInt(longitude)));
					} catch (NumberFormatException e) {
						business.setLongitude(null);
					}
			}
			if (property_name.matches("website")) {
				business.setWebsite(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("business_type")) {
				business.setType(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("more_info")) {
				business.setMore_info(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("pic_link")) {
				business.setPic_link(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("_source")) {
				business.setSource(property_lst.item(property_index).getTextContent());
			}
			if (property_name.matches("_source_type")) {
				business.setSource_type(property_lst.item(property_index).getTextContent());
			}

		}
		
		/*get deals*/
		List<Deal> deal_lst = new LinkedList<Deal>();
		NodeList deals = business_element.getElementsByTagName("deals");
		if (deals == null) return null;
		
		Element el_deals = (Element) deals.item(0);
		NodeList deals_lst = el_deals.getElementsByTagName("deal");
		if (deals_lst == null) return null;
		
		for (int deals_index = 0; deals_index < deals_lst.getLength(); deals_index++){
			Element el_deal = (Element) deals_lst.item(deals_index);
			Deal deal = getDeal(el_deal);
			deal_lst.add(deal);
		}
		
		business.setDeals(deal_lst);
		
		return business;
	}
	public static List<CheckIn> getCheckInList(InputStream stream) {
		if (stream == null) return null;
		
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			if (db == null) return null;
	
			//parse using builder to get DOM representation of the XML file
			Document dom = db.parse(stream);
			if (dom == null) return null;
			
			Element docEle = dom.getDocumentElement();
			if (docEle == null) return null;
			
			
			NodeList nl = docEle.getElementsByTagName("entity");
			if (nl == null) return null;
			
			int checkin_count = nl.getLength();
			
			List<CheckIn> result = new LinkedList<CheckIn>();
			
			for (int index = 0; index < checkin_count; index++) {
				
				Element el = (Element)nl.item(index);
				
				/*parsing each business*/
				CheckIn checkin = getCheckIn(el);
				
				/*add business to list*/
				if (checkin == null) continue; 
				result.add(checkin);
				
			}
			
			return result;
		
		/*Exception handler*/
		} catch (ExceptionInInitializerError e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	
	}
	
	public static Likes getLikesList(InputStream stream) {
		if (stream == null) return null;
		
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			if (db == null) return null;
	
			//parse using builder to get DOM representation of the XML file
			Document dom = db.parse(stream);
			if (dom == null) return null;
			
			Element docEle = dom.getDocumentElement();
			if (docEle == null) return null;
			
			
			NodeList nl = docEle.getElementsByTagName("entity");
			if (nl == null) return null;
			
			int user_count = nl.getLength();
			
			if (user_count == 0) return new Likes(); 
				
			Element el = (Element)nl.item(0);
				
			return getLikesList(el);
						
		/*Exception handler*/
		} catch (ExceptionInInitializerError e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	
	}


	private static Likes getLikesList(Element user_el) {
		NodeList property_lst = user_el.getElementsByTagName("property");
		if (property_lst == null) return null;
		String likes_str = "";
		String likes_int = "";
		
		for (int property_index = 0; property_index < property_lst.getLength(); property_index++) {
			String property_name = property_lst.item(property_index).getAttributes().item(0).getNodeValue();
			if (property_name.matches("likes")) {
				likes_str = property_lst.item(property_index).getTextContent();
			}
			if (property_name.matches("likes_int")) {
				likes_int = property_lst.item(property_index).getTextContent();
			}
		}
		
		List<String> likes_str_lst = Arrays.asList((likes_str.split("#")));
		List<String> likes_int_lst = Arrays.asList((likes_int.split("#")));
		
		Likes likes = new Likes();
		likes.likes_int = likes_int_lst;
		likes.likes_str = likes_str_lst;
		return likes;
	}
	/**
	 * parsing the xml we get from the server, for any number of businesses (includes 1)
	 * return null if fail.
	 * */
	public static List<Business> getBusinessList(InputStream xml) {
		
		if (xml == null) return null;
		
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			if (db == null) return null;
	
			//parse using builder to get DOM representation of the XML file
			Document dom = db.parse(xml);
			if (dom == null) return null;
			
			Element docEle = dom.getDocumentElement();
			if (docEle == null) return null;
			
			
			NodeList nl = docEle.getElementsByTagName("business");
			if (nl == null) return null;
			
			int business_count = nl.getLength();
			
			List<Business> result = new LinkedList<Business>();
			
			for (int index = 0; index < business_count; index++) {
				
				Element el = (Element)nl.item(index);
				
				/*parsing each business*/
				Business business = getBusiness(el);
				
				/*add business to list*/
				if (business == null) continue; 
				result.add(business);
				
			}
			
			return result;
		
		/*Exception handler*/
		} catch (ExceptionInInitializerError e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/******************************
	 * Objects to xml
	 */
	
	
	/**
	 * returns the xml format of the string
	 * if fails return null
	 */
	public static String commentToXml(Comment comment) {
		if (comment == null) return null;
		String xml_line;
		
		StringBuilder xml = new StringBuilder();
		
		/*comment header*/
		xml_line = String.format("<entity kind=\"Comment\" key=\"%s\">\n", comment.getKey());
		xml.append(xml_line);
		
		/*key*/
		String key = comment.getFull_key();
		xml_line = String.format("<key>%s</key>\n", key);
		xml.append(xml_line);
		
		/*deal_item*/
		String deal_key = comment.getDeal_item();
		xml_line = String.format("<property name=\"deal_item\" type=\"key\">%s</property>\n", deal_key);
		xml.append(xml_line);
		
		/*comment_date*/
		String date = comment.getDate_time();
		if (date == null) date = "";
		xml_line = String.format("<property name=\"comment_date\" type=\"string\">%s</property>\n", date);
		xml.append(xml_line);
		
		/*comment_str*/
		String comment_str = comment.getComment_str();
		if (comment_str == null) comment_str = "";
		xml_line = String.format("<property name=\"comment_str\" type=\"string\">%s</property>\n", comment_str);
		xml.append(xml_line);
		
		/*closing header*/
		xml_line = "</entity>\n";
		xml.append(xml_line);
		
		return xml.toString();

	}	
	
	/**
	 * returns the xml format of the string
	 * if fails return null
	 */
	public static String dealToXml(Deal deal){
		if (deal == null) return null;
		String xml_line;
		
		StringBuilder xml = new StringBuilder();
		
		/*deal header*/
		xml_line = String.format("<entity kind=\"Deal\" key=\"%s\">\n", deal.getKey());
		xml.append(xml_line);
		
		/*get key*/
		String key = deal.getFull_key();
		xml_line = String.format("<key>%s</key>\n", key);
		xml.append(xml_line);
		
		/*properties - according to name*/
		
		String business_key = deal.getBusiness_item();
		xml_line = String.format("<property name=\"business_item\" type=\"key\">%s</property>\n", business_key);
		xml.append(xml_line);
		
		/*days_bitmap*/
		int days_bitmap = deal.getDays_bitmap();
		xml_line = String.format("<property name=\"days_bitmap\" type=\"integer\">%d</property>\n", days_bitmap);
		xml.append(xml_line);
		
		/*deal_info*/
		String deal_info = deal.getDealInfo();
		if (deal_info == null) deal_info = "";
		xml_line = String.format("<property name=\"deal_info\" type=\"string\">%s</property>\n", deal_info);
		xml.append(xml_line);
		
		/*name*/
		String name = deal.getName();
		if (name == null) name = "";
		xml_line = String.format("<property name=\"name\" type=\"string\">%s</property>\n", name);
		xml.append(xml_line);
		
		/*rate_num*/
		int rate_num = deal.getRate_num();
		xml_line = String.format("<property name=\"rate_num\" type=\"integer\">%s</property>\n", rate_num);
		xml.append(xml_line);
		
		/*rate_num*/
		int rate_sum = deal.getRate_sum();
		xml_line = String.format("<property name=\"rate_sum\" type=\"integer\">%s</property>\n", rate_sum);
		xml.append(xml_line);
		
		/*time*/
		String time = deal.getTime();
		if (time == null) time = "";
		xml_line = String.format("<property name=\"time\" type=\"string\">%s</property>\n", time);
		xml.append(xml_line);
		
		/*time_delta*/
		String time_delta = deal.getDelta_time();
		if (time_delta == null) time_delta = "";
		xml_line = String.format("<property name=\"time_delta\" type=\"string\">%s</property>\n", time_delta);
		xml.append(xml_line);
		
		/*to_rate*/
		String to_rate = String.valueOf(deal.getTo_rate());
		xml_line = String.format("<property name=\"to_rate\" type=\"string\">%s</property>\n", to_rate);
		xml.append(xml_line);
		
		/*to_cost_level*/
		String to_cost_level = String.valueOf(deal.getTo_cost_level());
		xml_line = String.format("<property name=\"to_cost_level\" type=\"string\">%s</property>\n", to_cost_level);
		xml.append(xml_line);
		
		
		/*closing header*/
		xml_line = "</entity>\n";
		xml.append(xml_line);
		
		return xml.toString();
	}
	
	/**
	 * 
	 * @param business
	 * @return xml version of business (return null if fail)
	 */
	public static String businessToXml(Business business) {
		if (business == null) return null;
		String xml_line;
		
		StringBuilder xml = new StringBuilder();
		
		/*business header*/
		xml_line = "<business>\n";
		xml.append(xml_line);
		
		/*business info header*/
		xml_line = String.format("<info>\n<entity kind=\"Business\" key=\"%s\">\n", business.getKey());
		xml.append(xml_line);
		
		/*get key*/
		String key = business.getFull_key();
		xml_line = String.format("<key>%s</key>\n", key);
		xml.append(xml_line);
		
		/*properties - according to name*/
		
		/*business type*/
		String business_type = business.getType();
		if (business_type == null) business_type = "";
		xml_line = String.format("<property name=\"business_type\" type=\"string\">%s</property>\n", business_type);
		xml.append(xml_line);
		
		
		/*city*/
		String city = business.getCity();
		if (city == null) city = "";
		xml_line = String.format("<property name=\"city\" type=\"string\">%s</property>\n", city);
		xml.append(xml_line);
		
		/*country*/
		String country = business.getCountry();
		if (country == null) country = "";
		xml_line = String.format("<property name=\"country\" type=\"string\">%s</property>\n", country);
		xml.append(xml_line);
		
		/*full_address*/
		String full_address = business.getFullAddress();
		if (full_address == null) full_address = "";
		xml_line = String.format("<property name=\"full_address\" type=\"string\">%s</property>\n", full_address);
		xml.append(xml_line);
		
		/*latitude*/
		Integer latitude = business.getLatitude();
		String latitudeStr;
		if (latitude != null) {
			latitudeStr  = latitude.toString();
		} else {
			latitudeStr  = "";
		}
		xml_line = String.format("<property name=\"latitude\" type=\"string\">%s</property>\n", latitudeStr);
		xml.append(xml_line);
		
		/*longitude*/
		Integer longitude = business.getLongitude();
		String longitudeStr;
		if (longitude != null) {
			longitudeStr  = longitude.toString();
		} else {
			longitudeStr  = "";
		}
		xml_line = String.format("<property name=\"longitude\" type=\"string\">%s</property>\n", longitudeStr);
		xml.append(xml_line);
		
		/*more_info*/
		String more_info = business.getMore_info();
		if (more_info == null) more_info = "";
		xml_line = String.format("<property name=\"more_info\" type=\"string\">%s</property>\n", more_info);
		xml.append(xml_line);
		
		/*name*/
		String name = business.getName();
		if (name == null) name = "";
		xml_line = String.format("<property name=\"name\" type=\"string\">%s</property>\n", name);
		xml.append(xml_line);
		
		/*street*/
		String street = business.getStreet();
		if (street == null) street = "";
		xml_line = String.format("<property name=\"street\" type=\"string\">%s</property>\n", street);
		xml.append(xml_line);
		
		/*street_num*/
		String street_num = "";
		if (business.getStreetNum()!= -1)
			String.valueOf(business.getStreetNum());
		xml_line = String.format("<property name=\"street_num\" type=\"string\">%s</property>\n", street_num);
		xml.append(xml_line);
		
		/*type*/
		String type = business.getType();
		if (type == null) type = "";
		xml_line = String.format("<property name=\"type\" type=\"string\">%s</property>\n", type);
		xml.append(xml_line);
		
		/*website*/
		String website = business.getWebsite();
		if (website == null) website = "";
		xml_line = String.format("<property name=\"website\" type=\"string\">%s</property>\n", website);
		xml.append(xml_line);
		
		/*closing header*/
		xml_line = "</entity>\n</info>\n";
		xml.append(xml_line);
		
		/*start deals header*/
		xml_line = "<deals>\n";
		xml.append(xml_line);
		
		for (Deal deal : business.getDeals()) {
			xml_line = dealToXml(deal);
			if (xml_line == null) continue;
			xml.append(xml_line);
		}
		
		/*close deals header*/
		xml_line = "</deals>\n";
		xml.append(xml_line);

		
		/*close business header*/
		xml_line = "</business>\n";
		xml.append(xml_line);
		
		return xml.toString();
	}
	
	public static String produceXmlPage(List<Business> business_lst){
		StringBuilder xml_str = new StringBuilder();
		
		/*xml header*/
		xml_str.append(XML_HEADER);
		
		/*start root*/
		String xml_business;
		xml_str.append("<root>\n");
		for (Business business : business_lst) {
			xml_business = businessToXml(business);
			if (xml_business == null) continue;
			xml_str.append(xml_business);
		}
		xml_str.append("</root>");
		
		return xml_str.toString();
	}

	public static String getResult(InputStream stream) {
		
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			if (db == null) return null;

			//parse using builder to get DOM representation of the XML file
			Document dom = db.parse(stream);
			if (dom == null) return null;
			
			Element docEle = dom.getDocumentElement();
			if (docEle == null) return null;
			
			
			NodeList nl = docEle.getElementsByTagName("result");
			if (nl == null) return null;
			
			return nl.item(0).getTextContent();
		} catch (DOMException e) {
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
}
